const fs = require("fs");
const path = require("path");
const Mock = require("mockjs");
const JSON5 = require("json5");

// 读取json文件
function getJsonFile(filePath) {
  // 读取指定json文件
  var json = fs.readFileSync(path.resolve(__dirname, filePath), "utf-8");
  // 解析并返回
  return JSON5.parse(json);
}

module.exports = function(app) {
  // 监听http的请求
  if (process.env.MOCK == "true") {
    app.get("/user/userinfo", function(req, res) {
      var json = getJsonFile("./userinfo.json5");

      res.json(Mock.mock(json));
    });
  }
};
